package com.ux.effect
{
	import com.greensock.TweenMax;
	import com.greensock.data.TweenMaxVars;
	import com.ux.manager.StageManager;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.events.TimerEvent;
	import flash.filters.BlurFilter;
	import flash.utils.Timer;
	
	
	/**
	 * @author winnie
	 */	
	public class Snow extends Sprite
	{
		private var snowMakeTimer : Timer;
		
		private var wid : Number;
		private var hei : Number;
		private var snowMaxSize : Number;
		private var snowMakeInterval : Number;
		private var snowSpeedMax : Number;
		private var snowSpeedMin : Number;
		
		public function Snow( _wid:Number=500, _hei:Number=500, 
							  _snowMaxSize:Number = 10, _snowMakeInterval:Number = 0.5, 
							  _snowSpeedMax:Number = 20, _snowSpeedMin:Number = 5 )
		{
			super();
			
			wid = _wid;
			hei = _hei;
			snowMaxSize = _snowMaxSize;
			snowMakeInterval = _snowMakeInterval;
			snowSpeedMax = _snowSpeedMax;
			snowSpeedMin = _snowSpeedMin;
			
			snowMakeTimer = new Timer( snowMakeInterval*1000, 1 );
			snowMakeTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onSnowTimerComplete );
		}
		
		private function onSnowTimerComplete( e:TimerEvent ):void 
		{
			snowMakeTimer.start();
			makeSnow();
		}
		
		private function makeSnow():void
		{
			var len:int = 5;
			for (var i:int = 0; i < len; i++) 
			{
				var snow : Shape = getCreatedSnow();
				var tween : TweenMaxVars = new TweenMaxVars();
				var pt1 : Object = { x:snow.width*Math.random()*4+snow.x, y:(hei-snow.y)/4 + snow.y };
				var pt2 : Object = { x:snow.x, y:(hei-snow.y)/2 + snow.y };
				var pt3 : Object = { x:snow.width*Math.random()*2+snow.x, y:(hei-snow.y)/1.3 + snow.y };
				var pt4 : Object = { x:snow.x, y:hei+50 };
				
				tween.onComplete(removeSnow, [snow] );
				tween.bezierThrough([ pt1, pt2, pt3, pt4 ]);
				
				TweenMax.to( snow, Math.max( Math.random()*snowSpeedMax, snowSpeedMin ) , tween );
			}
		}
		
		
		
		private function removeSnow( snow:Shape ):void
		{
			removeChild( snow );
		}
		
		
		
		private function getCreatedSnow():Shape
		{
			var snow : Shape = new Shape();
			var snowSize : Number = Math.max( snowMaxSize/2, Math.random()*snowMaxSize );
			var snowPos : int = Math.floor( snowSize ) + 2;
			var blur : BlurFilter = new BlurFilter( snowSize,snowSize,Math.round( 1/snowSize*7 ));
			
			snow.graphics.beginFill(0xffffff, 1 );
			snow.graphics.drawCircle( snowPos,snowPos,snowSize );
			snow.graphics.endFill();
			snow.filters = [blur];
			snow.alpha = 0;
			
			TweenMax.to( snow, 0.5, {alpha:Math.min( snowSize/10, 0.6 )} );
			snow.x = Math.random()*wid;
			snow.y = Math.random()*hei/3-200;
			
			addChild( snow );
			
			return snow;
		}
		
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// api
		//
		//-----------------------------------------------------------------------------------
		
		public function play():void 
		{
			snowMakeTimer.start();
		}
		
		public function stop(_disappearDuration:Number=2):void 
		{
			snowMakeTimer.stop();
			
			var len:int = numChildren;
			for (var i:int = 0; i < len; i++) 
			{
				var sn : Shape = getChildAt( i ) as Shape;
				TweenMax.to( sn, _disappearDuration, {alpha:0} );
			}
		}
	}
}



